var preClass = 'imgNew3Obj_ '
function newImg3(className, options) {
  var imgNew3Obj = {
    slideActiveIndex: 1, // 轮播图下标  从1开始
    slides: $('.' + className + ' .slide-item'), // 轮播图item  dom集合
    dots: $('.' + className + ' .dot-item'), // 图例item
    arrows: $('.' + className + ' .arrow'), // 方向按钮
    timer: undefined, // 轮播图 计时器
    options: options // 传入的其他事件
  }
  window[preClass + className] = imgNew3Obj

  showSlides(imgNew3Obj, imgNew3Obj.slideActiveIndex)

  // 方向点击切换图片事件
  imgNew3Obj.arrows.click(function () {
    var index = imgNew3Obj.slideActiveIndex + $(this).data('arrow')
    showSlides(imgNew3Obj, index)
    console.log('arrow 方向点击事件')
  })
  // 图例点击切换图片事件
  imgNew3Obj.dots.click(function () {
    var index = $(this).index() + 1
    showSlides(imgNew3Obj, index)
    console.log('dots 图例点击事件')
  })
  // 图例移入切换  暂停图片事件
  imgNew3Obj.dots.mouseenter(function () {
    var index = $(this).index() + 1
    showSlides(imgNew3Obj, index)
    stop(imgNew3Obj)

    console.log('dots 图例移入事件')
  })

  // 图例移出继续图片事件
  imgNew3Obj.dots.mouseleave(function () {
    run(imgNew3Obj)
    console.log('dots 图例移出事件')
  })
  imgNew3Obj.slides.click(function () {
    console.log('图片点击事件')
    if (imgNew3Obj.options && imgNew3Obj.options.onclick) {
      imgNew3Obj.options.clickImg(this, imgNew3Obj)
    }
  })
}

function showSlides(imgNew3Obj, index) {
  imgNew3Obj.slideActiveIndex = index

  if (index > imgNew3Obj.slides.length) {
    imgNew3Obj.slideActiveIndex = 1
  }

  if (index < 1) {
    imgNew3Obj.slideActiveIndex = imgNew3Obj.slides.length
  }

  // $方法 移除active
  imgNew3Obj.slides.removeClass('active')
  imgNew3Obj.dots.removeClass('active')

  // $方法 添加active
  $(imgNew3Obj.slides[imgNew3Obj.slideActiveIndex - 1]).addClass('active')
  $(imgNew3Obj.dots[imgNew3Obj.slideActiveIndex - 1]).addClass('active')

  // 计时 5s 后下一张图片
  clearTimeout(imgNew3Obj.timer)
  imgNew3Obj.timer = setTimeout(function () {
    showSlides(imgNew3Obj, imgNew3Obj.slideActiveIndex + 1)
  }, 5000)
}

function stop(imgNew3Obj) {
  clearTimeout(imgNew3Obj.timer)
}

function run(imgNew3Obj) {
  imgNew3Obj.timer = setTimeout(function () {
    showSlides(imgNew3Obj, imgNew3Obj.slideActiveIndex + 1)
  }, 5000)
}
